#include "ros/ros.h"
#include "srv/primeHandler.h"

bool isPrime(int target) {
    for (int i = 2; i * i <= target; i++) {
        if (!(target % i)) {
            return false;
        }
    }
    return true;
}

// 相应方法
bool doReq(srv::primeHandler::Request& req,
          srv::primeHandler::Response& resp) {
    int target = req.target;

    ROS_INFO("Target Num : %d", target);

    if (target < 0) { return false; }

    resp.isPrime = isPrime(target);
    return true;
}

int main(int argc, char *argv[]) {
    setlocale(LC_ALL, "");
    ros::init(argc, argv, "publisher");
    ros::NodeHandle nodeHandle;
    ros::ServiceServer server = nodeHandle.advertiseService("srv_info", doReq);
    // 创建服务端
    ROS_INFO("The Publisher's Response Service Started.");
    ros::spin();
    return 0;
}
